package com.usian.controller;

import com.usian.pojo.User;
import com.usian.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/login")
public class LoginController {

    @Autowired
    UserService userService;

    @RequestMapping("/toLogin")
    public String toLogin(){
        return "login";
    }


    @RequestMapping("/login")
    public String login(User user, HttpSession session){
        if(user!=null){
            List<User> users = userService.findByUserName(user.getUserName(), user.getPassword());
            if(users.size()>0){
                User user1 = users.get(0);
                session.setAttribute("user",user1);
                return "redirect:/user/index";
            }
        }
        return "login";
    }
}
